package com.cxhome.cxinfox.productinfo;

import com.cxhome.cxinfox.QCellCore.ProvinceService;
import com.cxhome.cxinfox.provider.ProviderService;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.product.ProductInfo;
import com.traffic.managent.model.product.Provider;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.StringUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 供应商产品管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/productinfo")
public class ProductInfoController extends CxBaseController {

	private static final String path = "/pages/system/productinfo/productinfo_";

	/**
	 * 默认方法
	 */
	public void index() {
		list();
	}

	/**
	 * 查询供应商产品
	 */
	public void list() {
		ProductInfo model = getModelByAttr(ProductInfo.class);

		StringBuilder orderSql = new StringBuilder("");
		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  pi.createTime desc,pi.providerId desc");
		} else {
			orderSql.append(" order by ").append(orderBy.trim());
		}

		Page<ProductInfo> page = ProductInfoService.service.paginate(model, getPaginator(), orderSql);
		// 查询下拉框（供应商）
		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("providerId")));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}

	
	/**
	 * 跳转新增页面
	 */
	public void add() {
		setAttr("selectProvider", ProviderService.service.selectProvider(""));
		setAttr("selectProvince", ProvinceService.service.selectProvince(""));
		render(path + "add.html");
	}

	/**
	 * 查看详情
	 */
	public void view() {
		ProductInfo model = ProductInfo.dao.findById(getPara());
		Provider provider = Provider.dao.findById(model.getStr("providerId"));
		model.put("providerName", provider.getStr("providerName"));
		setAttr("model", model);
		render(path + "view.html");
	}

	/**
	 * 删除功能
	 */
	public void delete() {
		ProductInfo.dao.deleteById(getPara());
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		ProductInfo model = ProductInfo.dao.findById(getPara());

		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("providerId")));
		setAttr("selectProvince", ProvinceService.service.selectProvince(model.getStr("province")));
		setAttr("model", model);
		render(path + "edit.html");
	}

	/**
	 * 更新或者新增
	 */
	public void save() {
		String ids = getPara();
		ProductInfo model = getModel(ProductInfo.class);
		String userid = getSessionUser().getUseriD();

		ProductInfoService.service.saveOrUpdate(ids, model, userid);
		renderMessage("保存成功");
	}

}
